#delimit;
set more off;

use partydata_BJPS;

/* rescale ideology */

replace rightleft = rightleft/100;

sort cabinetcode;

egen formnum = group(cabinetcode);

by cabinetcode: generate partynum = _n;

tabulate partynum, generate(partydum);

egen maxpn = max(partynum);
local mpn = maxpn;

egen maxfn = max(formnum);
local mfn = maxfn;

/* expected number of observations after conversion */

sort formnum;
by formnum: egen maxpnfo = max(partynum);
generate npar = 0;
replace npar = maxpnfo if partynum == maxpnfo;
generate ncab = 2^(npar) - 1;
egen totalobs = total(ncab);
sum totalobs;

keep formnum partydum*;

save template_old, replace;

/* expansion */

clear;

forvalues j = 1/`mfn' {;
  use template_old;
  keep if formnum==`j';
  fillin partydum*;
  replace formnum = `j' if formnum == .;
  egen testsum = rowtotal(partydum1-partydum`mpn');
  drop if testsum == 0;
  drop _fillin testsum;
  if `j'>1 {;
    append using template;
  };
  save template, replace;
  clear;
};

use partydata_BJPS;

sort cabinetcode;

egen formnum = group(cabinetcode);

by cabinetcode: generate partynum = _n;

keep formnum partynum party_seatshare rightleft govtparty prevgovtparty previouspm;

reshape wide party_seatshare rightleft govtparty prevgovtparty previouspm, i(formnum) j(partynum);

egen medlr = rowmedian(rightleft*);
egen largeshare = rowmax(party_seatshare*);

forvalues k = 1/`mpn' {;
  replace party_seatshare`k' = 0 if party_seatshare`k' == .;
  replace previouspm`k' = 0 if previouspm`k' == .;
  replace rightleft`k' = 0 if rightleft`k' == .;
  replace govtparty`k' = 0 if govtparty`k' == .;
  replace prevgovtparty`k' = 0 if prevgovtparty`k' == .;
  generate median`k' = 0;
  replace median`k' = 1 if rightleft`k' == medlr;
  generate largest`k' = 0;
  replace largest`k' = 1 if party_seatshare`k' == largeshare;
  generate abs_med_diff`k' = abs(rightleft`k' - medlr);
};

merge 1:m formnum using template;

generate cab_seatshare = 0;
generate cab_median = 0;
generate cab_largest = 0;
generate cab_med_diff = 0;
generate cab_med_w_diff = 0;
generate numparties = 0;
generate ingovt_count = 0;
generate prevgovt_count = 0;
generate prevpm = 0;

forvalues m = 1/`mpn' {;
  replace party_seatshare`m' = party_seatshare`m' * partydum`m';
  replace cab_seatshare = cab_seatshare + party_seatshare`m';
  };

forvalues i = 1/`mpn' {;
  replace abs_med_diff`i' = abs_med_diff`i' * partydum`i';
  replace cab_med_diff = cab_med_diff + abs_med_diff`i';
  replace cab_med_w_diff = cab_med_w_diff + (abs_med_diff`i' * (party_seatshare`i' / cab_seatshare));
  replace median`i' = median`i' * partydum`i';
  replace cab_median = cab_median + median`i';
  replace largest`i' = largest`i' * partydum`i';
  replace cab_largest = cab_largest + largest`i';
  replace numparties = numparties + partydum`i';
  replace govtparty`i' = govtparty`i' * partydum`i';
  replace ingovt_count = ingovt_count + govtparty`i';
  replace prevgovtparty`i' = prevgovtparty`i' * partydum`i';
  replace prevgovt_count = prevgovt_count + prevgovtparty`i';
  replace previouspm`i' = previouspm`i' * partydum`i';
  replace prevpm = prevpm + previouspm`i';
  };
  
egen maxid_diff = rowmax(abs_med_diff*);

sort formnum;
by formnum: egen max_govtcount = max(ingovt_count);
by formnum: egen max_prevgcount = max(prevgovt_count);
generate govt_coal = 0;
replace govt_coal = 1 if ingovt_count == numparties & max_govtcount == numparties;
generate incumb_govt = 0;
replace incumb_govt = 1 if prevgovt_count == numparties & max_prevgcount == numparties;
generate incumb_parties = 0;
replace incumb_parties = 1 if prevgovt_count > 0;

/* round to reasonable number of digits */

replace maxid_diff = round(maxid_diff, .01);

/* no largest parties in case of tie */

sort formnum;
by formnum: egen large_count = max(cab_largest);
replace cab_largest = 0 if large_count > 1;

generate cab_minor = (cab_seatshare < 50);

generate cab_mwc = 1;
replace cab_mwc = 0 if cab_minor == 1;
forvalues p = 1/`mpn' {;
  replace party_seatshare`p' = . if party_seatshare`p' ==0;
};
egen seatshare_min = rowmin(party_seatshare*);
replace cab_mwc = 0 if (cab_seatshare - seatshare_min) > 50;

keep formnum govt_coal incumb_govt incumb_parties cab_med_diff cab_med_w_diff 
cab_seatshare cab_median cab_largest numparties prevpm ingovt_count cab_minor 
cab_mwc partydum*;

rename cab_med_diff distance_median;
replace distance_median = distance_median/100; /* note rescaling */
rename cab_med_w_diff distance_median_weighted;
replace distance_median_weighted = distance_median_weighted/100; /* note rescaling */
rename cab_seatshare seatshare;
generate seatshare_sq = (seatshare^2)/100; /* note rescaling */
rename cab_median median_party;
rename cab_largest largest_party;
rename cab_minor minor;
rename cab_mwc minwin;

generate govtparty = (ingovt_count > 0);

save coaldata_BJPS, replace;

